/**
 * 
 */
package toutiao;

import java.util.Scanner;
import java.util.Stack;

/**
 * @author zhongfang
 *
 */
public class 编程2 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			int[] nums = new int[n];
			for (int i = 0; i < n; i++){
				nums[i] = sc.nextInt();
			}
			long min =  Long.MIN_VALUE;

			Stack<Integer> stack = new Stack<>();
			for (int i = 0; i < n; i++) {
				if (stack.isEmpty()) {
					stack.add(i);
					continue;
				}
				while (!stack.isEmpty() && nums[i] <= nums[stack.peek()]) {//压出比i大的
					int index = stack.pop();
					long mid = calMinValue(nums, stack.isEmpty() ? -1 : stack.peek(), i, nums[index]);
					min = Math.max(mid, min);
				}
				stack.add(i);
			}

			while (!stack.isEmpty()) {
				int index = stack.pop();
				long mid = calMinValue(nums, stack.isEmpty() ? -1 : stack.peek(), n, nums[index]);
				min = Math.max(mid, min);
			}
			System.out.println(min);
		}
	}

	public static long calMinValue(int[] nums, int left, int right, int mul) {//计算所得值
		long sum = 0;
		for (int i = left + 1; i < right; i++)
			sum += nums[i];
		return sum * mul;
	}
}
